import java.text.SimpleDateFormat

class ProfitController {

    def index = { redirect(action:list,params:params) }

    def list = {
        def hql = "select p.sellTime,sum(p.pureProfits) from Profit p group by p.sellTime order by p.sellTime desc"
        def resultList = Profit.executeQuery(hql)
        [ profitInstanceList: resultList, profitInstanceTotal: resultList.size() ]
    }

    def weekColumnChar ={
        def hql ="select p.sellTime,sum(p.pureProfits) from Profit p where p.sellTime >= current_date()-7 and p.sellTime <= current_date() group by p.sellTime"
        def resultList = Profit.executeQuery(hql)
        if (resultList == [])
        {
            render "7天内没有商品售出，请返回"
        }
        def dataList = []
        resultList.each { s->
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd")
            def dateStr = sdf.format(s[0])
            def bar = ['date':dateStr, 'pureProfit':s[1]]
            dataList << bar
        }
        [profitChartList: dataList]
    }
}
